package com.xm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xm.common.util.PageUtil;
import com.xm.dao.ItemCarDao;
import com.xm.dao.UserDao;
import com.xm.pojo.ItemCar;
import com.xm.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemCarServiceImpl implements ItemCarService{
    @Autowired
    ItemCarDao itemCarDao;
    @Autowired
    UserDao userDao;


    @Override
    public void saveItemCar(String userName, Integer itemId) {
        int userId=getUserIdByUsername(userName);
        itemCarDao.insert(new ItemCar().setUserId(userId).setItemId(itemId));
    }

    @Override
    public void deleteItemCar(String userName, Integer itemId) {
        int userId=getUserIdByUsername(userName);
        ItemCar itemCar=new ItemCar().setItemId(itemId).setUserId(userId);
        itemCarDao.delete(new QueryWrapper<>(itemCar));
        System.out.println("删除成功");
    }

    @Override
    public PageInfo<Object> findItemByUserName(String userName) {
        int userId=getUserIdByUsername(userName);
        return PageUtil.startPage().doSelectPageInfo(()->itemCarDao.selectListByUserId(userId));
    }

    public int getUserIdByUsername(String username){
        User user= userDao.selectOne(new QueryWrapper<>(new User().setUsername(username)));
        return user.getId();
    }


}
